/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.dao.hibernate;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.integrationpath.mengine.dao.AjaxUtilityDao;
import com.integrationpath.mengine.dao.UtilityDao;
import com.integrationpath.mengine.model.AppUser;
import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.model.Comment;
import com.integrationpath.mengine.model.Rating;

public class AjaxUtilityDaoHibernate extends HibernateDaoSupport implements AjaxUtilityDao {

	public List getAllCategories() {
		return (List)getHibernateTemplate().find("from Categoriy c");
	}

	public List getAllTags() {
		return (List)getHibernateTemplate().find("from Tag t");
	}

	public Byte changeArticleStatus(Long articleId) {
		Session session = getSessionFactory().getCurrentSession();
	    Article article = (Article) session.load(Article.class, articleId);
	    Byte oldStatus = article.getStatus();
	    Integer newStatus = 1 - oldStatus.intValue();
	    article.setStatus(newStatus.byteValue());
	    session.update(article);
	    return newStatus.byteValue();
	}

	public Byte changeArticleFeatured(Long articleId) {
		Session session = getSessionFactory().getCurrentSession();
	    Article article = (Article) session.load(Article.class, articleId);
	    Byte oldFeatured = article.getFeatured();
	    Integer newFeatured = 1 - oldFeatured.intValue();
	    article.setFeatured(newFeatured.byteValue());
	    session.update(article);
	    return newFeatured.byteValue();
	}
	
	public Byte changeCommentStatus(Long commentId) {
		Session session = getSessionFactory().getCurrentSession();
	    Comment comment = (Comment) session.load(Comment.class, commentId);
	    Byte oldStatus = comment.getStatus();
	    Integer newStatus = 1 - oldStatus.intValue();
	    comment.setStatus(newStatus.byteValue());
	    session.update(comment);
	    return newStatus.byteValue();
	}

	public Object changeUserStatus(Long userId) {
		Session session = getSessionFactory().getCurrentSession();
	    AppUser appUser = (AppUser) session.load(AppUser.class, userId);
	    boolean value = !appUser.isAccountLocked();
	    appUser.setAccountLocked(value);
	    session.update(appUser);
	    return appUser.isAccountLocked();
	}
}
